package com.okami.web.order;

import com.okami.domain.order.Order;
import com.okami.domain.order.OrderRepository;
import com.okami.domain.order.OrderService;
import com.okami.domain.order.PaymentInformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.time.LocalDate;
import java.util.Optional;

/**
 * @author: TanJingyu
 * @create: 2020-12-07 10:24
 **/
@RestController
@RequestMapping("/test")
public class OrderTestController {
    private final OrderRepository orderRepository;
    private final OrderService orderService;
    private final DataSource dataSource;
    private Logger logger = LoggerFactory.getLogger(OrderTestController.class);

    public OrderTestController(OrderRepository orderRepository, OrderService orderService, DataSource dataSource) {
        this.orderRepository = orderRepository;
        this.orderService = orderService;
        this.dataSource = dataSource;
    }

    @GetMapping("/save")
    public ResponseEntity<Object> save() {
        orderRepository.save(new Order().setRemarks("李白").setState(Order.OrderState.CANCELLED).setPaymentInformation(new PaymentInformation("使用的支付宝")));
        return ResponseEntity.ok().build();
    }

    @GetMapping("/update/{orderId}")
    public ResponseEntity<Object> update(@PathVariable int orderId) {
        Optional<Order> order = orderRepository.findById(orderId);
        order.ifPresent(o -> orderRepository.save(o.setRemarks("修改了！")));
        return ResponseEntity.ok().build();
    }

    @GetMapping("/print-orderService")
    public String printOrderService() {
        return orderService.toString();
    }

    @GetMapping("/date")
    public Object date() {
        return LocalDate.now().minusDays(5);
    }

    @GetMapping("/datasource")
    public Object datasource() {
        logger.info(dataSource.toString());
        return "去控制台";
    }

}
